import {Window_Selectable} from '@/windows/Window_Selectable.js'
import { DataManager } from '@/managers/DataManager.js';
import { TextManager } from '@/managers/TextManager.js';

export class Window_ItemList extends Window_Selectable {
    constructor(width, height) {
        super(width, height,false);
        this.init(width,height);
    }

    init(width,height){
        this._category = '';
        this._list = [];
        this.trait_condition={
            [TextManager.item]:item=>DataManager.isItem(item) && item.itypeId === 1,
            [TextManager.weapon]:item=>DataManager.isWeapon(item),
            [TextManager.armor]:item=>DataManager.isArmor(item),
            [TextManager.keyItem]:item=>DataManager.isItem(item) && item.itypeId === 2
        }
        this.selectedTrait=null;
        this.okHandler=null;
        this.cancelHandler=null;
        this.changedHandler=null;

        this.numWidth=0;
        super.init(width,height);
    }

    setHandler(name,callback){
        if(name=='ok'){
            this.okHandler=callback;
        }else if(name=='cancel'){
            this.cancelHandler=callback;
        }else if(name=='changed'){
            this.changedHandler=callback;
        }
    }

    onResponse(index){this.okHandler?.(this._list[index],index);}
    onCancel(){this.cancelHandler?.();}
    onSelectChanged(index,oldIndex){
        this.changedHandler?.(this._list[index],index,this._list[oldIndex],oldIndex);
    }

    setCategory(category) {
        if (this._category !== category) {
            this._category = category;
            this.selectedTrait=this.trait_condition[category];
            this.refresh();
        }
    }

    column() {return 2;}
    viewColumn(){return 2;}

    row(){return Math.ceil(this.length()/2);}
    viewRow(){return Math.floor(this.contentsHeight()/this.itemHeight());}
    itemWidth(){return Math.floor(this.contentsWidth()/this.viewColumn());}
    length() {return this._list.length;}
    
    itemEnabled(index) {return super.itemEnabled(index)&&$gameParty.canUse(this._list[index]);}

    makeItemList() {
        if(this.selectedTrait){
            this._list = $gameParty.allItems().filter(item=>this.selectedTrait(item));
        }else{
            this._list.length=0;
        }
    }

    drawItem(index,x,y,width,height) {
        let item = this._list[index];
        if (item) {
            let nwidth=this.numberWidth();
            let enabled=this.itemEnabled(index);
            if(!enabled){
                this.changePaintOpacity(false);
            }
            this.drawItemName(item, x , y,width-nwidth);
            this.drawText($gameParty.numItems(item),x+width-nwidth, y, nwidth,height);
            if(!enabled){
                this.changePaintOpacity(true);
            }
        }
    }

    numberWidth() {
        if(!this.numWidth){
            this.numWidth=this.textWidth('000');
        }
        return this.numWidth;
    }

    refresh() {
        if(this._category){
            this.makeItemList();
            super.refresh();
        }
    }
}